package logic

import (
	"context"
	"edu-pro/edurpc/edurpc"
	"edu-pro/edurpc/internal/svc"
	"edu-pro/models/mysql"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectLogic {
	return &CollectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectLogic) Collect(in *edurpc.CollectRequest) (*edurpc.CollectResponse, error) {
	// 课程收藏功能
	var collect mysql.Collection
	mysql.DB.Debug().Where("user_id = ? AND course_id = ?", in.UserId, in.CourseId).Find(&collect)
	if collect.ID != 0 {
		return nil, errors.New("已收藏")
	}
	collect = mysql.Collection{
		UserId:   int(in.UserId),
		CourseId: int(in.CourseId),
	}
	mysql.DB.Debug().Create(&collect)
	return &edurpc.CollectResponse{
		CollectId: int64(collect.ID),
	}, nil
}
